import { TOKEN } from "@/common/const";
import storage from "@/utils/storage";
import { wxAgentConfig } from "@/utils/wxsdk";
import { useEffect, useState } from "react";

export const useWxAgentConfig = (jsApiList = [] as string[]) => {
    const [WWOpenData, setWWOpenData] = useState<any>(null);

    useEffect(() => {
        (async () => {
            try {
                if (!storage.get(TOKEN)) return;
                if(jsApiList.length === 0 && window.WWOpenData) {
                    setWWOpenData(window.WWOpenData);
                }
                else {
                    const data = await wxAgentConfig(jsApiList);
                    setWWOpenData(data);
                }
               
            } catch (error) {
                console.log('catch error')
                console.log(error);
            }
        })()
    }, []);



    return WWOpenData;
}

export default useWxAgentConfig;

